package com.huarun.module.sys.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.huarun.common.BaseController;
import com.huarun.common.ResponseMessage;
import com.huarun.common.ResultObj;
import com.huarun.common.util.ResultCodeEnum;
import com.huarun.module.sys.bean.Dict;
import com.huarun.module.sys.service.IDictService;

import io.swagger.annotations.ApiOperation;



/**
 * @author lilly
 * @catedDate 2018-04-27
 * @description 数据字典接口定义
 *
 */
@RestController
@RequestMapping(value = "/wx/dict")
public class DictController extends BaseController {

	@Resource
	private IDictService dictService;//注入service

	/**
	 * 根据ID获取数据字典信息
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "字典管理--根据ID获取数据字典信息", httpMethod = "GET", notes = "根据ID获取数据字典信息")
	@RequestMapping(value = "/getDict", method = RequestMethod.GET)
	public @ResponseBody ResultObj<Dict> getDictInfoById(@RequestParam(required = true) Integer id) {
		ResultObj<Dict> resultObj = new ResultObj<Dict>();
		try {
			Dict dict = dictService.getById(id);
			log.info("查询到数据字典信息");
			resultObj.setData(dict);
			resultObj.setErrorMessage("成功！");
		} catch (Exception e) {
			resultObj.setErrorCode(0);
			resultObj.setErrorMessage("查询数据字典信息出错!");
			log.error("查询数据字典信息出错!");
			e.printStackTrace();
		}
		return resultObj;
	}

	/**
	 * 新增数据字典
	 * @param dict
	 * @return
	 */
	@ApiOperation(value = "新增数据字典", httpMethod = "POST", notes = "新增数据字典")
	@RequestMapping(value = "/createDict", method = RequestMethod.POST)
	public @ResponseBody ResultObj<Map<String, String>> createDictInfo(@RequestBody Dict dict) {
		ResultObj<Map<String, String>> resultObj = new ResultObj<Map<String, String>>();
		try {
			int result = dictService.save(dict);
			log.info("新增{}条数据字典", result);
			resultObj.setErrorMessage("新增成功！");
		} catch (Exception e) {
			resultObj.setErrorCode(0);
			;
			resultObj.setErrorMessage("新增数据字典出错!");
			log.error("新增数据字典出错!");
			e.printStackTrace();
		}
		return resultObj;
	}

	/**
	 * 根据ID修改数据字典信息
	 * @param dict
	 * @return
	 */
	@ApiOperation(value = "根据ID修改数据字典信息", httpMethod = "PUT", notes = "根据ID修改数据字典信息")
	@RequestMapping(value = "/updateDict", method = RequestMethod.PUT)
	public @ResponseBody ResultObj<Map<String, String>> updateDictInfo(@RequestBody Dict dict) {
		ResultObj<Map<String, String>> resultObj = new ResultObj<Map<String, String>>();
		try {
			int result = dictService.save(dict);
			//int result = dictService.updateDictById(dict);
			log.info("更新{}条数据字典", result);
			//			resultObj.setData(dict);
			resultObj.setErrorMessage("更新成功！");
		} catch (Exception e) {
			resultObj.setErrorCode(0);
			;
			resultObj.setErrorMessage("更新数据字典出错!");
			log.error("更新数据字典出错!");
			e.printStackTrace();
		}
		return resultObj;
	}

	/**
	 * 根据传入参数修改数据字典信息
	 * @param dict
	 * @return
	 */
	@ApiOperation(value = "根据传入参数修改数据字典信息", httpMethod = "PUT", notes = "根据传入参数修改数据字典信息")
	@RequestMapping(value = "/updateDictSelective", method = RequestMethod.PUT)
	public @ResponseBody ResultObj<Map<String, String>> updateDictInfoSelective(@RequestBody Dict dict) {
		ResultObj<Map<String, String>> resultObj = new ResultObj<Map<String, String>>();
		try {
			int result = dictService.save(dict);
			log.info("更新{}条数据字典", result);
			//			resultObj.setData(dict);
			resultObj.setErrorMessage("更新成功！");
		} catch (Exception e) {
			resultObj.setErrorCode(0);
			;
			resultObj.setErrorMessage("更新数据字典出错!");
			log.error("更新数据字典出错!");
			e.printStackTrace();
		}
		return resultObj;
	}

	/**
	 * 根据ID删除数据字典
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除数据字典", httpMethod = "DELETE", notes = "删除数据字典")
	@RequestMapping(value = "/delDict", method = RequestMethod.DELETE)
	public @ResponseBody ResultObj<Dict> delDictInfoById(@RequestParam(required = true) Integer id) {
		ResultObj<Dict> resultObj = new ResultObj<Dict>();
		try {
			int result = dictService.delete(id);
			log.info("删除{}条数据字典", result);
			resultObj.setErrorMessage("删除成功！");
		} catch (Exception e) {
			resultObj.setErrorCode(0);
			;
			resultObj.setErrorMessage("删除数据字典出错!");
			log.error("删除数据字典出错!");
			e.printStackTrace();
		}
		return resultObj;
	}

	/**
	 * 根据类别获取数据字典列表
	 * @param dictCode
	 * @return
	 */
	@ApiOperation(value = "根据类别获取数据字典列表", httpMethod = "GET", notes = "根据类别获取数据字典列表")
	@RequestMapping(value = "/findDictListByType", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<List<Dict>> findDictListByType(@RequestParam(required = false) String dictCode) {
		ResponseMessage<List<Dict>> rm = new ResponseMessage<List<Dict>>();
		try {
			List<Dict> dictList = dictService.getDictListByType(dictCode);
		/*	Dict d = new Dict();
			d.setDictLabel("全部");
			d.setDictValue("");
			dictList.add(0, d);*/
			log.info("查询到{}条数据字典信息", dictList.size());
			rm.setData(dictList);
			rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
		} catch (Exception e) {
			rm.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
			log.error("查询数据字典信息出错！");
			e.printStackTrace();
		}
		return rm;
	}

}
